package team.szm.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import team.szm.scd.database.entity.Hospital;
import team.szm.scd.database.mapper.DoctorWorkTimeMapper;
import team.szm.scd.database.mapper.HospitalMapper;
import team.szm.scd.database.vo.WorkVO;
import team.szm.scd.service.interfaces.IHospitalService;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HospitalServiceImpl implements IHospitalService {
    @Resource
    HospitalMapper mapper;
    @Resource
    DoctorWorkTimeMapper workTimeMapper;

    @Override
    public List<Hospital> nameOrAddressLike(String name) {
        QueryWrapper<Hospital> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("hname", name)
                .or().like("haddress", name);
        return mapper.selectList(queryWrapper);
    }

    @Override
    public Hospital fromId(Integer id) {
        return mapper.selectById(id);
    }

    @Override//key1: 科室id, key2: 医生id, key3: 星期
    public Map<Integer, Map<String, Map<Integer, List<WorkVO>>>> workInfo(Integer id) {
        List<WorkVO> workVOList = workTimeMapper.workInfoFromId(id);
        Map<Integer, List<WorkVO>> m1 =
                workVOList.stream().collect(Collectors.groupingBy(WorkVO::getDepartmentId));
        Map<Integer, Map<String, Map<Integer, List<WorkVO>>>> result = new HashMap<>();
        m1.forEach((k, v) -> {
            Map<String, List<WorkVO>> m2 =
                    v.stream().collect(Collectors.groupingBy(WorkVO::getDoctorId));
            Map<String, Map<Integer, List<WorkVO>>> innerResult =
                    new HashMap<>();
            m2.forEach((k2, v2) -> innerResult.put(k2,
                    v2.stream().collect(Collectors.groupingBy(w -> w.getWeekDay().getValue()))));
            result.put(k, innerResult);
        });
        return result;
    }
}
